<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$allitemgame=$this->allitemgame;
if($_RQ['op']=='main'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$order=$this->session('order');
	if(!$order){
		exi('下单异常！','error');
	}
	$order['typeunit']=$allitemgame[$order['gamename']]['unit'];
	if($order['gamename']=='jy_merchgame_fx'){
		$order['typeunit']='购买';
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_videomerch/order//fromcreate=1');
	//提交数据
	$post = array();
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	foreach($order['goods'] as $k=>&$ov){
		$ov['image']=tomedia($ov['image']);
		if($ov['cartnum']<1){
			unset($order['goods'][$k]);
			continue;
		}
		$total+=$ov['cartnum'];
		$total_price+=$ov['cartnum']*$ov['price'];
	}
	$order['total']=$total;
	$order['total_price']=$total_price;
	//计算运费
	$order['address']=array();
	$order['dispatchprice']=0;
	if(!$order['sendtype']){
		$return['sendtypes']=array(
			'1'=>'快递配送',
			'2'=>'提货点自提',
		);
		$order['sendtype']=1;
	}
	if($order['merchid']){
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$order['merchid']);
		$return['carrier_list']=pdo_getall('jy_merch_tihuo',$params);
		foreach($return['carrier_list'] as &$item){
			$item['pcd']=json_decode($item['pcd'],true);
			if($item['isdefault']){
				$return['selectstore']=$item;	
			}
		}
		if(!$item['selectstore']){
			$return['selectstore']=$return['carrier_list'][0];	
		}
		model('jy_merch/member')->apiget_openid($order['merchid'],$_AR['access_json']);
	}else{
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		if($carrier_list){
			foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
			}
			$return['carrier_list']=$carrier_list;
			$return['selectstore']=$carrier_list['0'];
		}
	}
	if($order['sendtype']==1){
		if(intval($_RQ['addressid'])){
			$order['address']=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'id'=>intval($_RQ['addressid'])));
			$order['addressid']=intval($_RQ['addressid']);
		}else{
			$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
			if($member_address){
				foreach($member_address as &$address){
					if($address['isdefault']){
						$order['addressid']=$address['id'];
						$order['address']=$address;
						break;
					}
				}
				if(!$order['addressid']){
					$order['addressid']=$member_address['0']['id'];
					$order['address']=$member_address['0'];
				}
			}
		}
	}
	$return['address_list']=pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['address_item']=array();
	$post['total_price']=round($order['total_price'],2);
	$post['sendtype']=$order['sendtype'];
	$post['addressid']=$order['addressid'];
	$post['goods']=$order['goods'];
	$post['tihuo_id']=$return['selectstore']['id'];
	$lastorder=pdo_get('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'realname <>'=>'','mobile <>'=>''),'','id desc');
	if($lastorder['id']){
		$post['realname']=$lastorder['realname'];
		$post['mobile']=$lastorder['mobile'];
	}else{
		$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2','mobile','realname'));
		$post['realname']=$member2['realname'];
		$post['mobile']=$member2['mobile'];
	}
	$post['iscaoid']=$_RQ['iscaoid']?:0;
	$return['post'] = $post;
	$_SESSION['order']=$return['order']=$order;
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['realname'] || !$post['mobile']){
		exi('请填写联系人和联系电话！','error');
	}
	//整理订单数据
	$order=$_SESSION['order'];
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'merchid'=>$order['merchid'],
		'pid'=>$order['pid'],
		'gamename'=>$order['gamename'],
		'goods'=>$post['goods'],
		'canyuform'=>$post['canyuform'],
		'sendtype'=>$post['sendtype'],
		'addressid'=>$post['addressid'],
		'address'=>$post['address'],
		'realname'=>$post['realname'],
		'mobile'=>$post['mobile'],
		'remark'=>trim($post['remark']),
		'tihuo_id'=>$post['tihuo_id'],
	);
	$total=0;
	$total_price=0.00;
	foreach($post['goods'] as &$ov){
		$total+=$ov['cartnum'];
		$total_price+=$ov['cartnum']*$ov['price'];
	}
	$new_order['fee']=$total_price;
	if($new_order['sendtype']==1){
		//快递配送
		$new_order['addressid']=intval($post['addressid']);
		if(!$new_order['addressid']){
			exi('请先选择收货地址！','error');
		}		
	}elseif($new_order['sendtype']==2){
		//提货点自提
		if(!$new_order['tihuo_id']){
			exi('请先选择提货点！','error');
		}
	}elseif($new_order['sendtype']==3){
		//虚拟发货
	}
	$new_order['fee']=$new_order['fee']>0?$new_order['fee']:0;
	if($new_order['fee']<0){
		exi('订单金额不能小于零！','error');
	}
	if($post['iscaoid']){
		$new_order['api_data']['iscaoid']=$post['iscaoid'];
	}
	$order_id=model('jy_quanhuo/order')->create($new_order);
	unset($_SESSION['order']);
	$this->session('order','0');
	if($post['iscaoid']){
		pdo_update('jy_merch_game_fromuser',array('orderid'=>$order_id),array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['iscaoid']));
	}
	$typename=$allitemgame[$order['gamename']]['title'];
	$params=array(
		'id'=>$order_id,
		'title'=>$typename.'活动',
		'fee'=>$new_order['fee'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_videomerch/create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_videomerch/order/',
		'error_url'=>'jy_videomerch/order/',
	);
	if($new_order['merchid']){
		if(plugin('jy_merch')->setting['isone_credit']){
			$params['merchid']=$new_order['merchid'];
		}
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_merch_game_order',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_videomerch/order/');
	}
	$order=pdo_get('jy_merch_game_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_videomerch/order/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_videomerch/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_quanhuo/order')->order_pay($order['id'],$params);
	exi('提交成功!');
}

?>